package com.microbroadcast.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import com.microbroadcast.resource.Location;

public class LocationPersistence {

	public static Location getLocation(long locationID) {
		Location result = null;
		try {
			Connection c = DBHelper.getConnection();
			String sql = "select * from location where LocationID = ?";
			PreparedStatement ps = c.prepareStatement(sql);
			ps.setLong(1, locationID);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				String locationDescription = rs.getString("LocationDescription");
				String address = rs.getString("Address");
				float longitude = rs.getLong("longitude");
				float latitude = rs.getLong("latitude");
				Date createAt = rs.getDate("CreateAt");
				String city = CityPersistence.getCity(rs.getInt("CityID"));
				String province = ProvincePersistence.getProvince(rs.getInt("ProvinceID"));
				String country = rs.getString("Country");
				result = new Location(locationID, locationDescription,
						address, longitude, latitude, createAt,
						city, province, country);
			}
			rs.close();
			ps.close();
			c.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return result;
	}

}
